﻿/*******************************************************************************
* file  : rsa key text
* creat : apleilx
* data  : 2017/03/02
*******************************************************************************/
#include "common.h"
#include "crc_ext.h"
#include "mbedtls/rsa.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/platform.h"
#include "bsp_api.h"
#include "stdio_cfg.h"

extern void * rsa_src, *rsa_enc, *rsa_dec;

#define DEV_RANDOM_THRESHOLD        32
int rsa_random_entropy_poll( void *, unsigned char *, size_t, size_t *);
static const char seed_src[] = "rsa ce shi";

void dump_rsa_key(mbedtls_rsa_context *ctx);

/*******************************************************************************
* @brief  pub enc.
* \param[in] none
* \retval: none
*******************************************************************************/
void ras_pub_enc(void)
{
    int ret = 1;
    mbedtls_rsa_context rsa;
    mbedtls_entropy_context entropy;
    mbedtls_ctr_drbg_context ctr_drbg;
    mbedtls_mpi N, E;

	
    dbg_print( "\n  . Seeding the random number generator..." );
    fflush( stdout );

    mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
    mbedtls_rsa_init( &rsa);
    mbedtls_ctr_drbg_init( &ctr_drbg );
    mbedtls_entropy_init( &entropy );

	// 随机数源
	ret = mbedtls_entropy_add_source( 
		&entropy, 
		rsa_random_entropy_poll,
		NULL, 
		DEV_RANDOM_THRESHOLD,
		MBEDTLS_ENTROPY_SOURCE_STRONG );
	
    if(ret)
	{
        mbedtls_printf( " failed\n  ! mbedtls_entropy_add_source returned -0x%04x\n", (unsigned int) -ret );
		goto rsa_code_exit;
    }
	
    ret = mbedtls_ctr_drbg_seed( 
		&ctr_drbg, mbedtls_entropy_func,
		&entropy, (const unsigned char *) seed_src,
		strlen( seed_src ) );
	
    if( ret != 0 )
    {
        dbg_print( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n",ret );
        goto rsa_code_exit;
    }

	//ret = mbedtls_rsa_set_padding(&rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
	
	if(ret)
	{
		dbg_print("failed\n  ! mbedtls_rsa_set_padding returned %d\n",ret );
		goto rsa_code_exit;
	}
	
	// 读取公匙
    dbg_print( "\n  . Reading public key from rsa_pub_txt" );



	// 输入公匙
    if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 )
    {
        dbg_print( " failed\n  ! mbedtls_rsa_import returned %d\n\n",
                        ret );
        goto rsa_code_exit;
    }

	// 显示密匙
	dump_rsa_key(&rsa);
	
    // 计算编码值
    dbg_print( "\n  . Generating the RSA encrypted value" );
/*
    ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
                                     &ctr_drbg, 800, rsa_src, rsa_enc );
*/
    if( ret != 0 )
    {
        dbg_print( " failed\n  ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
                        ret );
        goto rsa_code_exit;
    }

    // 结果显示
	dbg_u8_print("\nrsa pub enc :\n", rsa_src, 1024, 32);
	dbg_u8_print("\nrsa pub dec :\n", rsa_dec, 1024, 32);

rsa_code_exit:
    mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
    mbedtls_ctr_drbg_free( &ctr_drbg );
    mbedtls_entropy_free( &entropy );
    mbedtls_rsa_free( &rsa );
}
